//
//  NewsAction.swift
//  letuofu
//
//  Created by Zack Zhang on 2020/5/13.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit

class NewsAction: NSObject {
    func getSyspushById(id: Int,callback: @escaping (Bool,[String:Any?]) -> Void) {
        let params :[String:Any] = ["id": id]
        API.formRequest(url: .YI_GetSyspushById, params: params) { (result) in
            if let dl = result.data?.getDictionary("data") {
                callback(result.success,dl)
            }
        }
    }
    
     func go(from:UIViewController, news:[String:Any?]) {
        if  let type = anyToInt(news["contentType"] ?? 0){
            switch type {
                case 0:
                //文本
                    if let content = news.getString("content") {
                        let vc = M_SimpleArticleVC()
                        from.pushViewController(vc)
                        vc.loadString(content)
                        vc.headTitle.text = "消息详情"
                    }
                return
                case 1:
                //1HTML
                    if let content = news.getString("content") {
                        let vc = M_SimpleArticleVC()
                        from.pushViewController(vc)
                        vc.loadString(content)
                        vc.headTitle.text = "消息详情"
                    }
                    else if let id = anyToInt(news["pushId"] as Any?) {
                        getSyspushById(id: id, callback: { (sec,data) in
                            if sec {
                                if let content = data.getString("content") {
                                    let vc = M_SimpleArticleVC()
                                    from.pushViewController(vc)
                                    vc.loadString(content)
                                    vc.headTitle.text = "消息详情"
                                }
                            }
                            
                        })
                    }


                return

                case 2:
                //红包
                return

                case 3:
                //文章
                    let vc = M_HuodongArticleVC()
                    from.pushViewController(vc)
                    vc.headTitle.text = news.getString("title")
                    vc.headTitle.text = "消息详情"
                    
                    if let did = news.getString("content") {
                        vc.artId = did

                    }
                return
                case 4:
                //商品详情
                    if let did = news.getString("content") {
                        let vc = M_ProductDetailVC(info: ["productNo":did])
                        from.pushViewController(vc)
                    }


                return
            default:
                return

            }

            
        }
        
    }
    
     func goPresent(from:UIViewController, news:[String:Any?]) {
        
        if  let type = anyToInt(news["contentType"] ?? 0){
            switch type {
                case 0:
                //文本
                    if let content = news.getString("content") {
                        let vc = M_SimpleArticleVC()
                        from.present(vc, animated: true, completion: nil)
                        vc.loadString(content)
                    }
                return
                case 1:
                //1HTML
                    if let content = news.getString("content") {
                        let vc = M_SimpleArticleVC()
                        from.present(vc, animated: true, completion: nil)
                        vc.loadString(content)
                    }


                return

                case 2:
                //红包
                return

                case 3:
                //文章
                    let vc = M_HuodongArticleVC()
                    from.present(vc, animated: true, completion: nil)
                    vc.headTitle.text = news.getString("title")
                    
                    if let did = news.getString("content") {
                        vc.artId = did

                    }
                return
                case 4:
                //商品详情
                    if let did = news.getString("content") {
                        let vc = M_ProductDetailVC(info: ["productNo":did])
                        from.present(vc, animated: true, completion: nil)
                    }


                return
            default:
                return

            }

            
        }
        
    }
}
